package com.dtsheep.easyexcel.service.local;

import com.alibaba.excel.EasyExcel;
import com.dtsheep.easyexcel.po.ExpressCompany;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Collections;

/**
 * OutputLocalService
 *
 * <p>
 *     文件导出
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/29
 */
@Component
public class ExportLocalService {

    public final static String EXPORT_BASE_PATH = "/Users/klys/project/java/easyexcel-spring-boot-starter/src/main/java/com/dtsheep/easyexcel/file/";

    /**
     * 本地文件导出
     * filePath 参数就是包含文件名的完整文件路径。例如，如果 filePath 是 /path/to/exportedFile.xlsx，那么导出的 Excel 文件名就会是 exportedFile.xlsx
     * 若文件已存在 则自动覆盖！！
     *
     */
    public static String exportByFile(){
        ExpressCompany expressCompany = new ExpressCompany();
        expressCompany.setCompany("测试公司");
        expressCompany.setCompanyNo("2343434");
        expressCompany.setCompanyType("国际");
        String exportPath = EXPORT_BASE_PATH + "导出测试.xlsx";
        File file = new File(exportPath);
        EasyExcel.write(file)
                .sheet("快递公司编码")
                .head(ExpressCompany.class)
                .doWrite(Collections.singletonList(expressCompany));
        return "导出成功";
    }

    public static void main(String[] args) {
        System.out.println(exportByFile());
    }
}
